package com.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.pojo.Student;
import com.req.PageReq;
import com.req.StudentPageReq;
import com.service.StudentService;
import com.utils.JwtUtils;
import com.utils.R;
import com.utils.UploadUtils;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/student")
@CrossOrigin
@Api(value = "student",tags = "student")
public class StudentController {
    @Resource
    StudentService studentService;

    // 登录验证 返回token
    @PostMapping("login")
    public R login(@RequestBody Student loginReq){
        String account = loginReq.getAccount();
        String password = loginReq.getPassword();
        Student student = studentService.login(account, password);
        String token = "";
        if (student != null) {
            // 生成token
            token = JwtUtils.getJwtToken(student.getId().toString(), student.getName());
        }else {
            return R.error().message("账号或密码错误");
        }
        return R.ok().data("token",token).data("user", student);
    }

    @GetMapping("/getInfo")
    public R info( HttpServletRequest httpRequest){
        String id = JwtUtils.getMemberIdByJwtToken(httpRequest);
        Student student = studentService.getById(Integer.valueOf(id));
        return R.ok().data("user", student);
    }

    @PostMapping("/logout")
    public R logout(){
        return R.ok();
    }

    @PostMapping("/register")
    public R register(@RequestBody Student student){
        boolean flag = studentService.register(student);
        if (flag){
            return R.ok();
        }else {
            return R.error().message("该用户已存在");
        }
    }


    // 查看用户列表
    @PostMapping("getStudentList")
    public R getStudentList(@RequestBody StudentPageReq studentPageReq){
        PageInfo<Student> pages = studentService.page(studentPageReq);
        return R.ok().data("pages",pages);
    }

    // 删除用户
    @DeleteMapping("deleteStudent")
    public R deleteStudent(@RequestBody List<Integer> ids){
        studentService.removeById(ids);
        return R.ok();
    }

    // 修改用户信息
    @PostMapping("updateStudent")
    public R updateStudent(@RequestBody Student student){
        studentService.updateById(student);
        return R.ok();
    }

    // 添加用户
    @PostMapping("addStudent")
    public R addStudent(@RequestBody Student student){
        boolean flag = studentService.save(student);
        if (flag){
            return R.ok();
        }else {
            return R.error().message("用户名已存在");
        }
    }

    // 查看用户信息
    @GetMapping("getStudentInfo")
    public R getStudentInfo(@RequestParam Integer id){
        Student studentInfo = studentService.getById(id);
        return R.ok().data("userInfo", studentInfo);
    }

    // 根据用户名查看用户是否存在
    @GetMapping("getUserByAccount")
    public R getUserByAccount(@RequestParam String account){
        Student student = studentService.getUserByAccount(account);
        if (student != null){
            return R.ok().data("user", student);
        }else {
            return R.error().message("用户不存在");
        }
    }

    // 根据用户名修改密码
    @PostMapping("updatePassword")
    public R updatePassword(@RequestBody Student student){
        boolean flag = studentService.updatePassword(student);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    // 校验密码是否正确
    @PostMapping("checkPassword")
    public R checkPassword(@RequestBody Student student){
        boolean flag = studentService.checkPassword(student);
        if (flag){
            return R.ok();
        }else {
            return R.error().message("原密码错误！请重试");
        }
    }

    // 已知密码 修改密码
    @PostMapping("changePassword")
    public R changePassword(@RequestBody Student student){
        boolean flag = studentService.changePassword(student);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    // 图片上传
    @PostMapping("uploadFile")
    public R uploadOssFile(MultipartFile file) {
        //获取上传文件  MultipartFile
        String url = UploadUtils.uploadFile(file);
        return R.ok().data("url",url);
    }

    // 查找专业列表
    @GetMapping("getMajorList")
    public R getMajorList(){
        List<String> majorList = studentService.getMajorList();
        return R.ok().data("majorList", majorList);
    }


    // 查找班级列表
    @GetMapping("getClassList")
    public R getClassList(){
        List<String> classList = studentService.getClassList();
        return R.ok().data("classList", classList);
    }



}
